package com.csy.stu.servlet;

import com.csy.stu.dao.StudentDao;
import com.csy.stu.dao.impl.StudentDaoImpl;
import com.csy.stu.vo.ResponseData;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author CSY
 * @date 2025 - 01 - 21
 */
@WebServlet("/api/student/delete_stu.do")
public class deleteStudentServlet extends BaseController{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        String sid = req.getParameter("sid");
        int intSid = Integer.parseInt(sid);

        StudentDao studentDao = new StudentDaoImpl();
        int n = studentDao.deleteById(intSid);

        ResponseData responseData = new ResponseData();
        responseData.setCode(200);
        responseData.setMsg("删除成功！");
        if(n <= 0){
            responseData.setCode(500);
            responseData.setMsg("删除失败！");
        }

        Gson gson = new Gson();
        String json = gson.toJson(responseData);

        PrintWriter out = resp.getWriter();
        out.print(json);
        out.flush();
        out.close();
    }
}
